﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using Yz.Base;
using Yz.Core.Extensions;
using Yz.DB.EFCore.Dal;
using Yz.Model.Constants;
using Yz.Model.Entities;
using Yz.Model.Enums;
using Yz.Model.Views;

namespace Yz.Web.Controllers
{
    [Authorize(Roles = UserRoleName.CompanyAdmin)]
    public class UserController : BaseController
    {
        private readonly IDbAccess<User> _userDbAccess;
        public UserController(IDbAccess<User> userDbAccess)
        {
            _userDbAccess = userDbAccess;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 分页查询员工信息
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="name"></param>
        /// <param name="loginName"></param>
        /// <param name="statusType"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetList(int pageSize, int pageIndex, string name, string loginName, StatusType statusType = StatusType.All)
        {
            var page = new ParaPage { SkipNum = (pageIndex - 1) * pageSize, TakeNum = pageSize };
            var orders = new List<ParaOrder<User>>() {
                new ParaOrder<User> { Order = p => p.UpdateTime  , IsAsc = false }
            };
            int companyId = base.CompanyId;
            Expression<Func<User, bool>> where = p => p.CompanyId == companyId && p.UserType == UserType.Staff;
            if (statusType != StatusType.All)
            {
                where = where.And(c => c.StatusType == statusType);
            }
            if (!loginName.IsEmpty())
            {
                where = where.And(c => c.LoginName.Contains(loginName));
            }
            if (!name.IsEmpty())
            {
                where = where.And(c => c.Name.Contains(name));
            }
            var list = await _userDbAccess.GetListByPageAsync<VmUser>(where, page, orders);
            return Json(list.ToVmPageList());
        }
        /// <summary>
        /// 添加修改用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<JsonResult> Save(User user)
        {
            if (user.Name.IsEmpty())
            {
                return Json(new VmReturn() { Suc = false, Msg = "姓名不能为空" });
            }
            if (user.LoginName.IsEmpty() || !user.LoginName.IsMobilePhone())
            {
                return Json(new VmReturn() { Suc = false, Msg = "账号请输入正确的手机号" });
            }
            var dtNow = DateTime.Now;
            user.UpdateTime = dtNow;
            var retCode = ReturnCode.Fail;
            int companyId = base.CompanyId;
            if (user.Id > 0)
            {
                retCode = await _userDbAccess.UpdateAsync(
                    p => p.SetProperty(q => q.LoginName, user.LoginName)
                    .SetProperty(q => q.Name, user.Name)
                    .SetProperty(q => q.StatusType, user.StatusType)
                    .SetProperty(q => q.UpdateTime, user.UpdateTime)
                    .SetProperty(q => q.Remark, user.Remark)
                    , p => p.Id == user.Id && p.CompanyId == companyId);
            }
            else
            {
                user.UserType = UserType.Staff;
                user.CompanyId = companyId;
                user.CreateTime = dtNow;
                user.LoginPwd = DefaultConstant.Password.ToMd5Hash();
                retCode = await _userDbAccess.AddAsync(user, p => p.LoginName == user.LoginName);
            }
            return Json(retCode.ToVmReturn());
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Delete(int id)
        {
            int companyId = base.CompanyId;
            if (id == 0)
            {
                return Json(new VmReturn() { Suc = false, Msg = "您要删除的用户不存在" });
            }
            var retCode = await _userDbAccess.DeleteAsync(p => p.Id == id && p.CompanyId == companyId);
            return Json(retCode.ToVmReturn());
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> RetSetPwd(int id)
        {
            int companyId = base.CompanyId;
            if (id == 0)
            {
                return Json(new VmReturn() { Suc = false, Msg = "您要操作的用户不存在" });
            }
            string pwd = DefaultConstant.Password.ToMd5Hash();
            var dtNow = DateTime.Now;
            var retCode = await _userDbAccess.UpdateAsync(p => p.SetProperty(q => q.LoginPwd, pwd).SetProperty(q => q.UpdateTime, dtNow), p => p.Id == id && p.CompanyId == companyId);
            return Json(retCode.ToVmReturn());
        }
    }
}